package com.sh.suhe.utildemos.getImageColor;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.palette.graphics.Palette;

import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;

import com.sh.suhe.utildemos.R;

import java.util.ArrayList;
import java.util.List;

public class ImageColorActivity extends AppCompatActivity {

    ImageView image, imageBg;
    ColorBgView colorBgView;

    Bitmap bgBitmap;
    Canvas mCanvas;
    Paint mPaint;
    private static final int[][] EMPTY = new int[][] { new int[0] };
    private int[] mColors;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_color);

        image = findViewById(R.id.image);
        imageBg = findViewById(R.id.image_bg);
        colorBgView = findViewById(R.id.image_bg_view);

        Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
        loadColor(bitmap);



    }
    private void loadColor(Bitmap bitmap) {
        if (bitmap == null) {
            return;
        }
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                //记得判空
                if(palette==null)return;
                //palette取色不一定取得到某些特定的颜色，这里通过取多种颜色来避免取不到颜色的情况
                if (palette.getDarkVibrantColor(Color.TRANSPARENT) != Color.TRANSPARENT) {
                    updateBgColor(palette.getDarkVibrantColor(Color.TRANSPARENT), palette.getVibrantColor(Color.TRANSPARENT));
                } else if (palette.getDarkMutedColor(Color.TRANSPARENT) != Color.TRANSPARENT) {
                    updateBgColor(palette.getDarkMutedColor(Color.TRANSPARENT), palette.getMutedColor(Color.TRANSPARENT));
                } else {
                    updateBgColor(palette.getLightMutedColor(Color.TRANSPARENT), palette.getLightVibrantColor(Color.TRANSPARENT));
                }
            }
        });
    }
    private void updateBgColor(int darkColor,int color) {
//        createLinearGradientBitmap(darkColor, color);
//        createLinearGradientColor(darkColor, color);
        setViewBg(darkColor, color);
    }
    private void setViewBg(int darkColor,int color) {
        colorBgView.setColor(darkColor, color);
    }
    private void createLinearGradientColor(int darkColor,int color) {
        GradientDrawable myShape = new GradientDrawable();//(GradientDrawable)imageBg.getBackground();
        mColors = new int[]{darkColor, color};
        ColorStateList colorStateList1 = new ColorStateList(EMPTY,mColors);

        myShape.setColor(colorStateList1);
        imageBg.setBackground(myShape);
    }

    //创建线性渐变背景色
    private void createLinearGradientBitmap(int darkColor,int color) {
        int bgColors[] = new int[2];
        bgColors[0] = darkColor;
        bgColors[1] = color;

        if(bgBitmap==null){
            bgBitmap= Bitmap.createBitmap(imageBg.getWidth(),imageBg.getHeight(), Bitmap.Config.ARGB_4444);
        }
        if(mCanvas==null){
            mCanvas=new Canvas();
        }
        if(mPaint==null){
            mPaint=new Paint();
        }
        mCanvas.setBitmap(bgBitmap);
        mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        LinearGradient gradient=new LinearGradient(0, 0, 0, bgBitmap.getHeight(),bgColors[0],bgColors[1], Shader.TileMode.CLAMP);
        mPaint.setShader(gradient);
        RectF rectF=new RectF(0,0,bgBitmap.getWidth(),bgBitmap.getHeight());
        // mCanvas.drawRoundRect(rectF,16,16,mPaint); 这个用来绘制圆角的哈
        mCanvas.drawRect(rectF,mPaint);
        imageBg.setImageBitmap(bgBitmap);
    }
}